package activiti2;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 排他网关（ExclusiveGateWay）
 */
public class PaiTaWangGuan {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署流程定义+启动流程实例
     *
     */

    @Test
    public void deployementAndStartProcess(){

        //部署流程定义
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("diagrams/paitawangguan.bpmn")
                .deploy();
        System.out.println("部署Id" + deployment.getId());

        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess_4");
        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义的ID："+pi.getProcessDefinitionId());

    }

    /**
     * 查询我的个人任务
     *
     * 部署Id32501
     * 流程实例ID：32505
     * 流程定义的ID：myProcess_4:1:32504
     * */
    @Test
    public void findPersonalTaskList(){
        //任务办理人
        String assignee = "张三";
        List<Task> list = processEngine.getTaskService()//
                .createTaskQuery()//
                .taskAssignee(assignee)//个人任务的查询
                .list();
        if(list!=null && list.size()>0){
            for(Task task:list){
                System.out.println("任务ID："+task.getId());
                System.out.println("任务的办理人："+task.getAssignee());
                System.out.println("任务名称："+task.getName());
                System.out.println("任务的创建时间："+task.getCreateTime());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("#######################################");
            }
        }
    }


    /**
     * 完成任务
     *
     */

    @Test
    public void completeTask(){
        //任务ID
        String taskId = "32508";
        //完成任务的同时，设置流程变量，让流程变量判断连线该如何执行
        Map<String,Object> map = new HashMap<>();
        map.put("money",800);
        processEngine.getTaskService().complete(taskId,map);

        System.out.println("完成任务："+taskId);
    }


}
